package com.suray.system.run.task;

import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.system.run.type.TaskType;
import com.suray.wcs.service.system.service.impl.TaskDBServiceImpl;
import com.suray.wcs.service.util.WcsUtil;

public class TaskMonitor {
    private TaskDBServiceImpl taskDBService = SpringContextHolder.getBean(TaskDBServiceImpl.class);

    private TaskInformation inCargoTaskInfo;

    private TaskInformation outCargoTaskInfo;

    private TaskInformation transTaskInfo;

    public TaskMonitor() {

    }

    public void init() {
        inCargoTaskInfo = new TaskInformation(com.suray.wcs.service.enums.TaskType.IN);
        outCargoTaskInfo = new TaskInformation(com.suray.wcs.service.enums.TaskType.OUT);
        transTaskInfo = new TaskInformation(com.suray.wcs.service.enums.TaskType.TRANSFER);

        monitorTask();
    }

    /**
     * 监控任务
     */
    private void monitorTask() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        inCargoTaskInfo.count();
                        outCargoTaskInfo.count();
                        transTaskInfo.count();

                        WcsUtil.delay(5000);
                    } catch (Exception e) {
                        WcsUtil.exceptionLog(e, this.getClass());
                    }
                }
            }
        }).start();
    }

    public void run() {

    }

    private TaskInformation findTaskInfo(TaskType taskType) {
        switch (taskType) {
            case IN:
                return inCargoTaskInfo;
            case OUT:
                return outCargoTaskInfo;
            case TRANSFER:
                return transTaskInfo;
            default:
                break;
        }

        return null;
    }

    public int getFinishTaskNum(TaskType taskType) {
        TaskInformation taskInformation = findTaskInfo(taskType);
        return taskInformation.getFinishTaskNum();
    }

    public int getExecingTaskNum(TaskType taskType) {
        TaskInformation taskInformation = findTaskInfo(taskType);
        return taskInformation.getExecingTaskNum();
    }

    public int getCancelTaskNum(TaskType taskType) {
        TaskInformation taskInformation = findTaskInfo(taskType);
        return taskInformation.getCancelTaskNum();
    }

    public int getErrorTaskNum(TaskType taskType) {
        TaskInformation taskInformation = findTaskInfo(taskType);
        return taskInformation.getErrorTaskNum();
    }

    public int getWaitForExecTaskNum(TaskType taskType) {
        TaskInformation taskInformation = findTaskInfo(taskType);
        return taskInformation.getWaitForExecTaskNum();
    }
}
